/**
    ******************************************************************************
    * @file    threadPool_test.c
    * @author  debian
    * @version V1.0.0
    * @date    2020-07-31
    * @brief   线程池测试函数
    ******************************************************************************
    * @attention
    *
    *
    ******************************************************************************
    */ 

/* Includes ------------------------------------------------------------------*/
#include "threadPool_test.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "threadPool.h"

/** @addtogroup DataStruct_Driver
    * @{
    */

/** @addtogroup threadPool_test
    * @{
    */

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/


/** @defgroup threadPool_test_Exported_Functions threadPool_test Exported Functions
    * @{
    */

/** @defgroup threadPool_test_Exported_Functions_Group1 Initialization and deinitialization functions
    *  @brief    Initialization and Configuration functions
    *
@verbatim    
    ===============================================================================
                ##### Initialization and deinitialization functions #####
    ===============================================================================
    [..]
        This section provides functions allowing to initialize and de-initialize the threadPool_test
        to be ready for use.
 
@endverbatim
    * @{
    */ 

static void* write_file(void* argv)
{
    char path[128] = {0};
    int file_index = *((int*)argv);

    //please check there is test directory under your current directory
    snprintf(path, 128, "test/%d.txt", file_index);
 
    FILE* file_ptr = fopen(path, "w+");
    if (file_ptr != NULL)
    {
        char *str="hello,I am a test program!\r\n";
        for(int i = 0; i < 100; i++)
        {
            fwrite(str, sizeof(char), strlen(str), file_ptr);
        }

        fclose(file_ptr);
    }
    else
    {
        printf("write file failed.\n");
    }

    return NULL;
}
    
    int main(int argc, char *argv[])
    {
        if(argc != 2) {
            printf("param error\n");
            return -1;
        }

        int task_count = atoi(argv[1]);

        printf("thread test\n");

        threadPool_t *pool = threadPool_creat(8, 200, threadpool_cond_type);
        if(pool == NULL) {
            printf("pool is null\n");
            return -1;
        }

        int *file_index = malloc(sizeof(int) * task_count);
        for(int i=0; i<task_count; i++) {
            file_index[i] = i;
        }

        for(int i=0; i<task_count; i++) {
            if(0 != threadPool_add(pool, write_file, (void*)&file_index[i])) {
                printf("add task to thread pool failed\n");
            }
        }
        printf("while\n");
        while(1);

        return 0;
    }
/**
    * @}
    */

/** @defgroup threadPool_test_Exported_Functions_Group2 operation functions 
    *  @brief   operation functions
    *
@verbatim   
    ===============================================================================
                        ##### operation functions #####
    ===============================================================================
    [..]
        This subsection provides a set of functions allowing to manage the threadPool_test.

@endverbatim
    * @{
    */

        /* 操作函数写在这里 */

    /**
    * @}
    */


/**
    * @}
    */

/**
    * @}
    */

/**
    * @}
    */

/************************ (C) 2020 HOME 深圳龙华 *****END OF FILE****/

